import {ref, onMounted} from 'vue'
// 将img转为base64
const tobase64 = (img:HTMLImageElement) => {
    const canvas = document.createElement('canvas')
    canvas.width = img.width
    canvas.height = img.height
    const ctx = canvas.getContext('2d')
    ctx?.drawImage(img, 0,0,canvas.width, canvas.height)

    // document.body.prepend(canvas)
    return canvas.toDataURL('image/png',0.1)
}
const use2Base64 = (imgSelector:string) => {
    return new Promise<{basestr: string}>((resolve) => {
        const basestr = ref<string>('')
        onMounted(() => {
            const img:HTMLImageElement = document.querySelector(imgSelector) as HTMLImageElement
            
            img.onload = () => {
                console.log('图片加载完毕');
                basestr.value = tobase64(img)
                resolve({
                    basestr: basestr.value
                })
            }
        })
    })
     

   
}
export {use2Base64}